#include "zhuce.h"
#include "ui_zhuce.h"

#include "YanZhengMa.h"
#include <QLineEdit>
#include "sql.h"
#include "config.h"

ZhuCe::ZhuCe(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::ZhuCe)
{
    ui->setupUi(this);

    // 创建一个标签用于显示背景图像
        QLabel *backgroundLabel = new QLabel(this);
        QPixmap backgroundImage(":/new/prefix1/bjt.png"); // 加载背景图像
        backgroundLabel->setPixmap(backgroundImage);
        backgroundLabel->setScaledContents(true); // 缩放背景图像以填充标签

        // 设置标签的位置和大小以覆盖整个窗口
        backgroundLabel->setGeometry(0, 0, this->width(), this->height());

        // 将标签放置在窗口的底层
        backgroundLabel->lower();


}

ZhuCe::~ZhuCe()
{
    delete ui;
}

void ZhuCe::on_fasongyanzhengma_clicked()
{
    QLineEdit *EmailEdit = findChild<QLineEdit*>("youxiang");

    QString Email = EmailEdit->text();
    qDebug()<<Email;

    QString captcha = EmailService::generateCaptcha();
    QString recipient = Email; // 收件人邮箱地址
    if (EmailService::sendCaptchaEmail(recipient, captcha)) {
        qDebug() << "验证码发送成功.";
    } else {
        qDebug() << "验证码发送失败.";
    }
}


void ZhuCe::on_zhuce_clicked()
{
    qDebug()<<"确认注册";
        QLineEdit *R_username = findChild<QLineEdit*>("zhanghao");
        QLineEdit *R_password = findChild<QLineEdit*>("mima");
        QLineEdit *R_email = findChild<QLineEdit*>("youxiang");
        QString username = R_username->text();
        QString password = R_password->text();
        QString email = R_email->text();
    //    qDebug()<<username;
    //    qDebug()<<password;
    //    qDebug()<<email;
        QJsonDocument jsonRegistration = createJsonRegistration(username, password, email);

        // 将 JSON 数据转换为字符串以便存储或传输
        QString jsonString = jsonRegistration.toJson();
        qDebug() << "JSON 数据：" << jsonString; // 输出 JSON 数据字符串

          if (SqlController::getInstance()->registerUser(username, password, email)) {
              qDebug() << "注册成功";
          } else {
              qDebug() << "注册失败";
          }
}


void ZhuCe::on_fanhui_clicked()
{
    emit backToLogin(); // 发射返回登录界面的信号
}

